﻿using System.Collections.Generic;
using System.Linq;
using Model;

namespace DAL
{
    public class City
    {
        public static IList<Model.ViewModel.City> SelectAll()
        {
            var citys = DbHelper.Db.Citys.ToList();
            return ClassConvert<Model.ViewModel.City, Model.City>.Convert(citys);
        }

        public static Model.ViewModel.City SelectById(int cityId)
        {
            var citys = DbHelper.Db.Citys.Find(cityId);//Find 根据主键查找
            return ClassConvert<Model.ViewModel.City, Model.City>.Convert(citys);
        }

        public static IList<Model.City> SelectByName(string cityName)
        {
            return DbHelper.Db.Citys.Where(c => c.CityName == cityName).ToList();
        }

        public static void Add(Model.City city)
        {
            DbHelper.Db.Citys.Add(city);
            DbHelper.Db.SaveChanges();
        }

        public static void Delete(int id)
        {
            var city = DbHelper.Db.Citys.Find(id);
            DbHelper.Db.Citys.Remove(city);
            DbHelper.Db.SaveChanges();
        }

        public static void Save()
        {
            DbHelper.Db.SaveChanges();
        }
    }
}
